package com.vctgo.subject.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.utils.poi.ExcelUtil;
import com.vctgo.common.core.web.controller.BaseController;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.log.annotation.Log;
import com.vctgo.common.log.enums.BusinessType;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vctgo.subject.domain.TbSubject;
import com.vctgo.subject.service.ITbSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 科目/期初Controller
 *
 * @author zhx
 * @date 2024-12-16
 */
@RestController
@RequestMapping("/subject")
public class TbSubjectController extends BaseController
{
    @Autowired
    private ITbSubjectService tbSubjectService;

    /**
     * 查询科目/期初列表
     */
    @RequiresPermissions("subject:subject:list")
    @GetMapping("/list")
    public AjaxResult list(TbSubject tbSubject)
    {

        Long subjectStatus = tbSubject.getSubjectStatus();
        tbSubject.setSubjectStatus((subjectStatus == null || "".equals(subjectStatus))? 1L : (subjectStatus == 1? 1L : (subjectStatus == 0? null : subjectStatus)));
            //11111111111111111111111111111
//        if(tbSubject.getSubjectStatus()==null || "".equals(tbSubject.getSubjectStatus()))
//        {
//            tbSubject.setSubjectStatus(1L);
//        }else {
//
//            if(tbSubject.getSubjectStatus()==1){
//                tbSubject.setSubjectStatus(1L);
//            }
//            if(tbSubject.getSubjectStatus()==0){
//                tbSubject.setSubjectStatus(null);
//            }
//        }

        IPage<TbSubject> list = tbSubjectService.selectTbSubjectPage(tbSubject);
        return AjaxResult.success(list);
    }

    /**
     * 导出科目/期初列表
     */
    @RequiresPermissions("subject:subject:export")
    @Log(title = "科目/期初", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbSubject tbSubject)
    {
        List<TbSubject> list = tbSubjectService.selectTbSubjectList(tbSubject);
        ExcelUtil<TbSubject> util = new ExcelUtil<TbSubject>(TbSubject.class);
        util.exportExcel(response, list, "科目/期初数据");
    }

    /**
     * 获取科目/期初详细信息
     */
    @RequiresPermissions("subject:subject:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbSubjectService.selectTbSubjectById(id));
    }

    /**
     * 获取科目/期初详细信息
     */

    @GetMapping("/selectSubject")
    public AjaxResult selectSubject()
    {
        List<TbSubject> tbSubjects = tbSubjectService.selectSubjectList();
        return success(tbSubjects);
    }

    /**
     * 新增科目/期初
     */
    @RequiresPermissions("subject:subject:add")
    @Log(title = "科目/期初", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbSubject tbSubject)
    {
        return toAjax(tbSubjectService.insertTbSubject(tbSubject));
    }

    /**
     * 修改科目/期初
     */
    @RequiresPermissions("subject:subject:edit")
    @Log(title = "科目/期初", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbSubject tbSubject)
    {
        return toAjax(tbSubjectService.updateTbSubject(tbSubject));
    }

    /**
     * 删除科目/期初
     */
    @RequiresPermissions("subject:subject:remove")
    @Log(title = "科目/期初", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbSubjectService.deleteTbSubjectByIds(ids));

    }



}
